﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
using DBA.Data;
using System.IO;

namespace DBA.Web.PaperManage
{
    public partial class OpenExcel : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
            string filePath = "/upload/PaperExcel/";

            string absolutPath = MapPath(filePath);
            if (!Directory.Exists(absolutPath))
            {
                Directory.CreateDirectory(absolutPath);
            }

            
            for (int i = 0; i < files.Count; i++)
            {
                if (files[i].FileName != "")
                {
                    string fileCode = DateTime.Now.ToString("yyyyMMddHHmmssfff") + files[i].FileName.Substring(files[i].FileName.LastIndexOf('.'));
                    //上传文件到服务器

                    string strFileFullPath = absolutPath + fileCode;
                    files[i].SaveAs(strFileFullPath);
                    ImportExcel(strFileFullPath);
                    //读入服务器上面的excel文件到内存中


                }
            }
        }

        private bool ImportExcel(string fileName) 
        {
            bool flag = true;
            string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + fileName + ";" + "Extended Properties=Excel 8.0;HDR=Yes;IMEX=1";
            string strSql = "SELECT 论文名称 FROM [Sheet1$]";
            OleDbDataAdapter ExcelDA = new OleDbDataAdapter(strSql,strConn);

            DataSet ExcelDs = new DataSet();

            try
            {
                ExcelDA.Fill(ExcelDs, "Paper");

                DataTable dtPaper = ExcelDs.Tables["Paper"];


            }

            catch (System.Exception ex) 
            {
                flag = false;
            }



            return flag;
        }
    }
}